﻿Imports System.Web.Security
Imports System.Web.UI
Imports System.Data.OleDb
Imports System.Data.Entity.Validation

Public Class sms_moneyjunction

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
            bindmoneydata()
            bindtag()
            ' bindgrid()
        End If


    End Sub

    Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs)
        Dim id As Integer
        id = Convert.ToInt32(GridView1.DataKeys(e.NewEditIndex).Value)



        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
        Dim SqlString As String = "select * from sms_moneytagjunction where ID =  @ID"

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)


        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@ID", id)

        oleDbConn.Open()

        Dim sda As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        Dim ds As DataSet = New DataSet()
        sda.Fill(ds)


        Dim dr As DataRow = ds.Tables(0).Rows(0)

        ddl_moneydata.SelectedValue = dr("sms_moneydata_fkid").ToString()
        ddl_tag.SelectedValue = dr("sms_tag_fkid").ToString()
        hdf.Value = dr("ID").ToString()
        Disable()
        Button1.Text = "Edit"



    End Sub
    Private Sub Disable()
        ddl_moneydata.Enabled = False
        ddl_tag.Enabled = False
    End Sub

    Private Sub Enable()
        ddl_moneydata.Enabled = True
        ddl_tag.Enabled = True
    End Sub
    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        Try
            If Button1.Text = "Edit" Then
                Enable()
                Button1.Text = "Save"
            ElseIf Button1.Text = "Save" Then

                If (ddl_moneydata.SelectedValue <> "0" And ddl_tag.SelectedValue <> "0") Then
                    If (hdf.Value <> "") Then
                        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
                        Dim SqlString As String = "update sms_moneytagjunction set sms_moneydata_fkid=@moneydata, sms_tag_fkid=@tag where  ID =  @ID"

                        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)


                        cmd.CommandType = CommandType.Text
                        cmd.Parameters.AddWithValue("@moneydata", ddl_moneydata.SelectedValue)
                        cmd.Parameters.AddWithValue("@tag", ddl_tag.SelectedValue)
                        cmd.Parameters.AddWithValue("@ID", hdf.Value)

                        oleDbConn.Open()
                        cmd.ExecuteNonQuery()

                        'bindgrid()
                        'Disable()
                        Button1.Text = "Edit"
                        GridView1.EditIndex = -1
                        ddl_moneydata.SelectedValue = "0"
                        ddl_tag.SelectedValue = "0"
                        hdf.Value = ""
                        GridView1.DataSourceID = "SqlDataSource005"
                        GridView1.DataBind()

                    End If
                End If

            End If

        Catch ex As DbEntityValidationException

        End Try



    End Sub
    Protected Sub btncancel_Click(sender As Object, e As EventArgs)
        Button1.Text = "Edit"
        Disable()


    End Sub

    Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
        Try
            Dim id As Integer
            id = Convert.ToInt32(GridView1.DataKeys(e.RowIndex).Value)

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
            Dim SqlString As String = "delete  FROM sms_moneytagjunction where ID=@ID"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@ID", id)
            cmd.CommandType = CommandType.Text
            oleDbConn.Open()
            cmd.ExecuteNonQuery()
            ' bindgrid()


        Catch ex As DbEntityValidationException

        End Try
    End Sub
    Protected Sub bindmoneydata()
        Try
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
            Dim SqlString As String = "select * from sms_moneydata"

            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            oleDbConn.Open()
            Dim sda As OleDbDataAdapter = New OleDbDataAdapter(cmd)
            Dim ds As DataSet = New DataSet()
            sda.Fill(ds)
            ddl_moneydata.DataTextField = "Company_Name"
            ddl_moneydata.DataValueField = "ID"
            ddl_moneydata.DataSource = ds
            ddl_moneydata.DataBind()
            ddl_moneydata.Items.Insert(0, New ListItem("--Select--", "0"))
        Catch ex As Exception

        End Try
    End Sub
    Protected Sub bindtag()
        Try
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
            Dim SqlString As String = "select * from sms_tags"

            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            oleDbConn.Open()
            Dim sda As OleDbDataAdapter = New OleDbDataAdapter(cmd)
            Dim ds As DataSet = New DataSet()
            sda.Fill(ds)
            ddl_tag.DataTextField = "Tag"
            ddl_tag.DataValueField = "ID"
            ddl_tag.DataSource = ds
            ddl_tag.DataBind()
            ddl_tag.Items.Insert(0, New ListItem("--Select--", "0"))
        Catch ex As Exception

        End Try
    End Sub
    Private Sub bindgrid()
        Try
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString001").ConnectionString)
            Dim SqlString As String = "SELECT sms_moneytagjunction.ID, sms_tags.Tag AS sms_tag_fkid, sms_moneydata.Company_Name AS sms_moneydata_fkid FROM  ((sms_moneytagjunction INNER JOIN sms_tags ON sms_moneytagjunction.sms_tag_fkid = sms_tags.ID) INNER JOIN sms_moneydata ON sms_moneytagjunction.sms_moneydata_fkid = sms_moneydata.ID)"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            oleDbConn.Open()
            Dim sda As OleDbDataAdapter = New OleDbDataAdapter(cmd)
            Dim ds As DataSet = New DataSet()
            sda.Fill(ds)

            GridView1.DataSource = ds
            GridView1.DataBind()



        Catch ex As DbEntityValidationException

        End Try

    End Sub


End Class